package com.guli.edu.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.vo.R;
import com.guli.edu.entity.Course;
import com.guli.edu.form.CourseInfoForm;
import com.guli.edu.query.CourseQuery;
import com.guli.edu.service.CourseService;
import com.guli.edu.vo.CoursePublishVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XiaoHua666
 * @date 2019/4/23
 */
@Api(tags = "课程管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/course")
public class CourseAdminController {
    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "新增课程")
    @PostMapping("/save-course-info")
    public R saveCourseInfo(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoForm courseInfoForm) {
        String courseId = courseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("courseId", courseId);
    }

    @GetMapping("course-info/{id}")
    @ApiOperation(value = "查询单条课程记录", notes = "根据id查找课程记录")
    public R getCourseById(
            @ApiParam(value = "课程id", name = "id", required = true)
            @PathVariable("id") String id
    ) {
        CourseInfoForm courseInfoForm = courseService.getCourseFormInfoById(id);
        return R.ok().data("item", courseInfoForm);
    }

    @ApiOperation(value = "更新课程记录")
    @PutMapping("update-course-info/{id}")
    public R updateCourseInfo(
            @ApiParam(value = "更新课程信息", name = "CourseInfoForm", required = true)
            @RequestBody CourseInfoForm courseInfoForm,
            @ApiParam(value = "课程Id")
            @PathVariable("id") String id
    ) {
        courseService.updateCourseInfoById(courseInfoForm);
        return R.ok();
    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(value = "当前页码", name = "page", required = true)
            @PathVariable("page") Long page,
            @ApiParam(value = "每页记录数", name = "limit", required = true)
            @PathVariable("limit") Long limit,
            @ApiParam(value = "查询对象", name = "courseQuery", required = false)
                    CourseQuery courseQuery
    ) {

        Page<Course> pageParam = new Page<>(page, limit);
        courseService.pageQuery(pageParam, courseQuery);
        List<Course> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("rows", records).data("total", total);
    }

    @ApiOperation(value = "删除课程记录")
    @DeleteMapping("delete-course-info/{id}")
    public R removeById(@ApiParam(value = "课程id") @PathVariable("id") String id) {
        courseService.deleteCourseById(id);
        return R.ok();
    }

    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("course-publish-info/{id}")
    public R getCoursePublishVoById(
            @ApiParam(value = "课程id", name = "id")
            @PathVariable String id
    ) {
        CoursePublishVo coursePublishVoById = courseService.getCoursePublishVoById(id);


        return R.ok().data("item", coursePublishVoById);
    }

    @ApiOperation(value = "根据id发布课程")
    @PutMapping("publish-course/{id}")
    public R publishCourseById(
            @ApiParam(value = "课程id", name = "id")
            @PathVariable("id") String id
            ) {
        courseService.publishCourseById(id);
        return R.ok();
    }


}
